#include "restorewidget.h"
#include <ibase.h>

#define RESPBUF	1024

RestoreWidget::RestoreWidget()
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);

fbrestore = NULL;
dbDestLabel->setText( QLatin1String("") );

QObject::connect(restRunToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(run_()));
QObject::connect(restCancelToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(close_restore()));
QObject::connect(DBDestButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(setDBFileName()));
QObject::connect(backNameToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(setBackupFileName()));
                 
}

RestoreWidget::~RestoreWidget()
{
}

void RestoreWidget::closeEvent(QCloseEvent * e)
{
    if (NULL != fbrestore)
    {
        if ( true == fbrestore->isRunning() )
        {
        QMessageBox::warning(this,
                    tr("Error"),
                    tr("Restore is running") );
        e->ignore();
        }
        else
        {
        fbrestore->disconnect();
        delete fbrestore;
        fbrestore = NULL;
        e->accept();
        }
    }
    else
    {
    e->accept();
    }
}

void RestoreWidget::run_()
{
    if (NULL != fbrestore)
    {
        if (true == fbrestore->isRunning() )
        {
        QMessageBox::warning( this,
                              tr("Error"),
                              tr("Restore is running") );
        return;
        }
        else
        {
        fbrestore->disconnect();
        delete fbrestore;
        fbrestore = NULL;
        }
        
    }
fbrestore = new FBRestore();

QObject::connect( fbrestore,
                  SIGNAL( log_line(QString) ),
                  this,
                  SLOT( write_log(QString) ) );
QObject::connect( fbrestore,
                  SIGNAL( finished() ),
                  this,
                  SLOT( enable_rbutton() ) );                  

QString back_file_ = backNameLineEdit->text(); 
    if ( true == back_file_.isEmpty() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("No backup file selected") );
    return;
    }

    if ( true == restEmbCheckBox->isChecked() )
    {
    QString back_file = QDir::toNativeSeparators(back_file_);
    QString db_file_ = QCoreApplication::applicationDirPath();
    db_file_.append( QDir::separator() );
    db_file_.append( QLatin1String("COLL0.FDB") );
    QString db_file = QDir::toNativeSeparators(db_file_);
    fbrestore->set_config( back_file,
                           db_file,
                           QLatin1String("SYSDBA"),
                           QLatin1String("masterp"),
                           QLatin1String("service_mgr" ),
                           false );
    }
    else
    {
    QString serv_name;
        if (true == restServiceCheckBox->isChecked())
        {
        serv_name = restHostLineEdit->text();
        serv_name.append( QLatin1String(":") );
        serv_name.append( restServLineEdit->text() );
        }
        else
        {
        serv_name = QLatin1String("service_mgr"); 
        }

    
    QString user = QLatin1String("");
    QString pass = QLatin1String("");
        if ( true == restCredCheckBox->isChecked() )
        {
        user = restUsrLineEdit->text();
        pass = restPasswdLineEdit->text();
        }

    QString out_file_ = dbDestLabel->text();
        if ( true == out_file_.isEmpty() )
        {
        QMessageBox::warning( this,
                              tr("Error"),
                              tr("No database selected") );
        return;
        }

    QString out_file = QDir::toNativeSeparators(out_file_); 
    QString back_file = QDir::toNativeSeparators(back_file_);

    fbrestore->set_config( back_file,
                           out_file,
                           user,
                           pass,
                           serv_name,
                           false );
    }                  
fbrestore->start();
restRunToolButton->setDisabled(true);
restCancelToolButton->setDisabled(true);
}

void RestoreWidget::write_log(QString s)
{
restLogTextBrowser->insertPlainText(s);
}

void RestoreWidget::enable_rbutton()
{
    if (true != restRunToolButton->isEnabled())
    {
    restRunToolButton->setDisabled(false);
    restCancelToolButton->setDisabled(false);
    }

    if (NULL != fbrestore)
    {
    fbrestore->disconnect();
    delete fbrestore;
    fbrestore = NULL;
    }
}

void RestoreWidget::close_restore()
{
    if (NULL != fbrestore)
    {
        if (true == fbrestore->isRunning() )
        {
        QMessageBox msgBox;
        msgBox.setText( tr("Abort runnung restore?!\nApplication will close.") );
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            switch (msgBox.exec())
            {
                case QMessageBox::Yes:
                      if (NULL != fbrestore)
                      {
                          if (true == fbrestore->isRunning() )
                          {
                          fbrestore->disconnect();
                          fbrestore->terminate();
                          fbrestore->exit(1);
                          }
                      }
                close();
                qApp->exit(1);
                break;

                default:
                break;
            }
        }
        else
        {
        fbrestore->disconnect();
        delete fbrestore;
        fbrestore = NULL;
        QMessageBox::information( this,
                                  tr("Information"),
                                  tr("Reconnect to DB") );
        close();
        }
    }
    else
    {
    QMessageBox::information( this,
                              tr("Information"),
                              tr("Reconnect to DB") );    
    close();
    }
return;    
}

void RestoreWidget::setDBFileName()
{
QString fileName = QFileDialog::getSaveFileName(this,
                   tr("Select database"),
                   dbDestLabel->text(),
                   tr("Database files (*.fdb);;All Files (*)"));
    if (!fileName.isEmpty())
    {
    dbDestLabel->setText(fileName);
    }
}

void RestoreWidget::setBackupFileName()
{
QString path = QLatin1String("");
QString home_path = QDir::homePath();
QString desktop_path = home_path;
desktop_path.append( QDir::separator() );
desktop_path.append( QLatin1String("Desktop") );
QDir desktop_dir(desktop_path);
    if ( true == desktop_dir.exists() )
    {
    path = QDir::toNativeSeparators(desktop_path);
    }
    else
    {
    path = QDir::toNativeSeparators(home_path);
    }
QString fileName = QFileDialog::getOpenFileName(this,
                   tr("Select backup file"),
                   path,
                   tr("Backup files (*.fbk);;All Files (*)"));
    if ( !fileName.isEmpty() )
    {
    backNameLineEdit->setText(fileName);
    }
}
